<%@ Control Language="C#" ClassName="PurchaseBatches" Inherits="Dimok.Web.UI.TableControl" %>
<script RunAt="server">
	public int ContractHolderID { 
		set {
			ds.ContractHolderID = value;
			SelectedIndex = -1;
		} 
	}
	public int BatchID {
		set {
			SelectFilter = "ID=" + value;
			SetFilterBoxes("ID=" + value);
			//SelectedIndex = 0;
			RequiresDataBinding = true;
			PurchaseContractsByBatch.BatchID = value;
		}
	}
	protected override void OnLoad(EventArgs e) {
		base.OnLoad(e);
		this.RowSelected+=new GridViewExEventHandler(PurchaseBatches_RowSelected);
		grv.SearchChanged += new GridViewEx.SearchChangedEventHandler(grv_SearchChanged);
		PurchaseContractsByBatch.RowDeleting += new GridViewDeleteEventHandler(PurchaseContractsByBatch_RowDeleting);
	}

	void BtnImage_Command(object sender, CommandEventArgs e) {
		Button btnImage = (Button)sender;
		FileUpload fu = btnImage.Parent.FindControl("uplImage") as FileUpload;
		OrderedDictionary keys = new OrderedDictionary();
		keys["ID"] = btnImage.CommandArgument;
		new Data.DataSources.PurchaseBatchesDataSource().UpdateField(keys, "BatchImages", fu.FileBytes, false);
	}

	void FileUpload_PreRender(object sender, EventArgs e) {
		ScriptManager sm = Dimok.Web.UI.Find.ChildByType(Page, typeof(ScriptManager)) as ScriptManager;
		sm.RegisterPostBackControl((Control)sender);
	}

	void PurchaseContractsByBatch_RowDeleting(object sender, GridViewDeleteEventArgs e) {
		e.Cancel = true;
		new PurchaseBatch_LoansDataSource().DeleteByLoanID((int)e.Keys[0]);
	}

	void grv_SearchChanged(string search) {
		grv.SelectedIndex = -1;
		grv.RequiresDataBinding = true;
		PurchaseContractsByBatch.BatchID = 0;
	}

	void PurchaseBatches_RowSelected(object sender, Dimok.Web.UI.GridViewExEventArgs e) {
		PurchaseContractsByBatch.BatchID = e.Values.Count > 0 ? int.Parse("0"+e.Values["ID"]) : 0;
	}
</script>
<?XML:NAMESPACE PREFIX="COOL"/>
<style>
cool\:tip{behavior: url(/Include/HTC/cooltip_js.htc)}
</style>
<table>
<tr><td>
<ajaxtk:CollapsiblePanelExtender runat="server"
	TargetControlID="pnlCBT"
	CollapseControlID="pnlCBTHeader" ExpandControlID="pnlCBTHeader"
	TextLabelID="lblCBTLabel"
	ImageControlID="imgCBTPanel"
	CollapsedImage="~/images/Buttons/expand.jpg" ExpandedImage="~/images/Buttons/collapse.jpg"
	Collapsed="false"	CollapsedText="" ExpandedText=""
	SuppressPostBack="true"
></ajaxtk:CollapsiblePanelExtender>
<asp:Panel ID="pnlCBTHeader" CssClass="PanelHeaderBlue" style="width:100%" runat="server">
<table cellPadding="0" cellSpacing="0"><tr>
<th Width="1px"><asp:Image ID="imgCBTPanel" ImageUrl="~/images/Buttons/expand.jpg" runat="server" />
<td nowrap><label ID="lblCBTLabel" runat="server" style="text-align:center">Purchase Batches</label></td>
</tr></table>
</asp:Panel> 
<asp:Panel ID="pnlCBT" runat="server" CssClass="PannelContent">

<asp:UpdatePanel ID="pnlRebateOffers" UpdateMode="Always" runat="server">
	<ContentTemplate>
		<UI:GridViewEx ID="grv" runat="server" AutoGenerateColumns="False" DataSourceID="ds"
			AllowSorting="True"	DataKeyNames="ID" EnableViewState="false"
			>
			<Columns>

<asp:TemplateField HeaderText="ID" SortExpression="ID">
	<ItemTemplate><span Class="TipMe" coolTip='<%#new PurchaseBatch_StatusHistoryDataSource().ShowToolTip(int.Parse("0"+Eval("ID")))%>' runat="server" ><%#Eval("ID")%></span></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contract Holder" SortExpression="ContractHolderNick">
	<ItemTemplate><%#Eval("ContractHolderNick") %></ItemTemplate>
	<EditItemTemplate><asp:TextBox Text='<%# Bind("ContractHolderNick") %>' ID="txtContractHolderNick" runat="server" /></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number" SortExpression="BatchNumber">
	<ItemTemplate><%#Eval("BatchNumber") %></ItemTemplate>
	<EditItemTemplate><asp:TextBox Text='<%# Bind("BatchNumber") %>' ID="txtBatchNumber" runat="server" /></EditItemTemplate>
	<FooterTemplate><asp:TextBox ID="BatchNumber" runat="server" /></FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" SortExpression="DateCreated">
	<ItemTemplate><%#Eval("DateCreated","{0:d}") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Loans" SortExpression="Name">
	<ItemTemplate><%#Eval("LoansCount") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="Status">
	<ItemTemplate><%#Eval("Status") %></ItemTemplate>
	<EditItemTemplate>fsdui sdiufy
		<DS:StateFlowNextStatesDataSource ID="dsSN" StateFromID='<%#int.Parse("0"+Eval("StatusID"))%>' runat="server" />
		<asp:DropDownList SelectedValue='<%#Bind("StatusID")%>' ID="selStatusID" DataSourceID="dsSN" DataTextField="StateTo" DataValueField="StateToID" runat="server"/>
	</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status Date" SortExpression="StatusDate">
	<ItemTemplate><%#Eval("StatusDate") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status User" SortExpression="StatusUser">
	<ItemTemplate><%#Eval("StatusUser") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload Image" SortExpression="">
	<ItemTemplate>
		<asp:PlaceHolder runat="server" visible='<%#Eval("BatchImages")!=DBNull.Value%>'>
		<input type="button" Value="Show Images" onclick='showImages(<%#Eval("ID")%>);event.cancelBubble=true;return;' />
		</asp:PlaceHolder>
	</ItemTemplate>
	<EditItemTemplate>
		<asp:FileUpload ID="uplImage" style="width:auto" runat="server" onclick="event.cancelBubble=true;return;" />
		<asp:Button ID="btnImage" UseSubmitBehavior="false" OnClientClick="event.cancelBubble=true;" style="width:auto" Text="Send" OnPreRender="FileUpload_PreRender" OnCommand="BtnImage_Command" CommandArgument='<%#Eval("ID")%>' runat="server" />
	</EditItemTemplate>	
</asp:TemplateField>
<asp:TemplateField HeaderText="Show Docs">
	<ItemTemplate>
		<input type="button" Value="Show Docs" onclick='showDocs(<%#Eval("ID")%>);event.cancelBubble=true;return;' />
	</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Show Docs">
	<ItemTemplate>
		<input type="button" Value="Show Letters" onclick='showLetters(<%#Eval("ID")%>);event.cancelBubble=true;return;' />
	</ItemTemplate>
</asp:TemplateField>
			</Columns>
		</UI:GridViewEx>
	</ContentTemplate>
</asp:UpdatePanel>

<LOAN:PurchaseContracts ID="PurchaseContractsByBatch" HideColumns="PurchaseBatchID,ContractHolder" ShowDelete="true" EmptyRowPolicy="None" BatchID="0" runat="server" />

</asp:Panel>
</table>
<DS:PurchaseBatchesDataSource ID="ds" OrderBys="ID DESC" runat="server" />
<script>
function showImages(batchID){
	var w = open("/Manager/Documents/FQuest/BatchImages.aspx?batchid="+batchID,"BATCH_IMAGES");
	try{	w.focus();}catch(e){}
}
function showDocs(batchID){
	var w = open("/Manager/Documents/FQuest/BatchPackage.aspx?batchid="+batchID,"BATCH_DOCS");
	try{	w.focus();}catch(e){}
}
function showLetters(batchID){
	var w = open("/Manager/Documents/FQuest/BatchLetters.aspx?batchid="+batchID,"BATCH_LETTERS");
	try{	w.focus();}catch(e){}
}
</script>